/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cc.siyecao.mapper;

import cc.siyecao.mapper.batch.BatchMapper;
import cc.siyecao.mapper.cursor.CursorMapper;
import cc.siyecao.mapper.entity.EntityMapper;
import cc.siyecao.mapper.entity.EntityProvider;
import cc.siyecao.mapper.function.Fn;
import cc.siyecao.mapper.lambda.LambdaMapper;
import cc.siyecao.mapper.provider.Caching;
import cc.siyecao.mapper.wrapper.LambdaWrapper;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Lang;

import java.io.Serializable;
import java.util.List;

/**
 * 基础 CloverMapper 方法，可以在此基础上继承覆盖已有方法
 *
 * @param <T>  实体类类型
 * @param <ID> 主键类型
 * @author liuzh
 */
public interface CloverMapper<T, ID extends Serializable>
        extends EntityMapper<T, ID>, BatchMapper<T, ID>, LambdaMapper<T, LambdaWrapper<T>>, CursorMapper<T, LambdaWrapper<T>> {

    /**
     * 保存实体，默认主键自增，并且名称为 id
     * <p>
     * 这个方法是个示例，你可以在自己的接口中使用相同的方式覆盖父接口中的配置
     *
     * @param entity 实体类
     * @return 1成功，0失败
     */
    @Override
    @Lang(Caching.class)
    @InsertProvider(type = EntityProvider.class, method = "insert")
    int insert(T entity);

    /**
     * 保存实体中不为空的字段，默认主键自增，并且名称为 id
     * <p>
     * 这个方法是个示例，你可以在自己的接口中使用相同的方式覆盖父接口中的配置
     *
     * @param entity 实体类
     * @return 1成功，0失败
     */
    @Override
    @Lang(Caching.class)
    @InsertProvider(type = EntityProvider.class, method = "insertSelective")
    int insertSelective(T entity);

    /**
     * 根据指定字段集合查询：field in (fieldValueList)
     * <p>
     * 这个方法是个示例，你也可以使用 Java8 的默认方法实现一些通用方法
     *
     * @param field          字段
     * @param fieldValueList 字段值集合
     * @param <F>            字段类型
     * @return 实体列表
     */
    default <F> List<T> selectByFieldList(Fn<T, F> field, List<F> fieldValueList) {
        LambdaWrapper<T> wrapper = new LambdaWrapper<>();
        wrapper.in( field, fieldValueList );
        return selectByWrapper( wrapper );
    }

    /**
     * 根据指定字段集合删除：field in (fieldValueList)
     * <p>
     * 这个方法是个示例，你也可以使用 Java8 的默认方法实现一些通用方法
     *
     * @param field          字段
     * @param fieldValueList 字段值集合
     * @param <F>            字段类型
     * @return 实体列表
     */
    default <F> int deleteByFieldList(Fn<T, F> field, List<F> fieldValueList) {
        LambdaWrapper<T> wrapper = new LambdaWrapper<>();
        wrapper.in( field, fieldValueList );
        return deleteByWrapper( wrapper );
    }

}
